//
//  MoyaAppendHeadersPlugin.swift
//  jiaoyi
//
//  Created by wjk on 2018/9/6.
//  Copyright © 2018年 wjk. All rights reserved.
//

import Moya
import Result


/// 新增Oauth 2.0的header使用

public struct MoyaAppendHeadersPlugin: PluginType {
    public func prepare(_ request: URLRequest, target: TargetType) -> URLRequest {
        var request = request
        if let urlString = request.url?.absoluteString, urlString.hasPrefix(AppConstants.apiEndpoint) {
            
            let authToken = UserCredsManager.authToken as String
            if authToken.count>0 {
                request.setValue(String(format: "Bearer %@", authToken), forHTTPHeaderField: "Authorization")
            }            
        }
        return request
    }
    
    public func didReceive(_ result: Result<Moya.Response, Moya.MoyaError>, target: TargetType) {
    }
}
